package study.springcloud.discovery.consul.support.consul;

import com.ecwid.consul.v1.agent.model.NewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistrationCustomizer;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.List;

@Slf4j
public class MyConsulRegistrationCustomizer implements ConsulRegistrationCustomizer {

    private ConfigurableEnvironment environment;

    public MyConsulRegistrationCustomizer(ConfigurableEnvironment environment) {
        this.environment = environment;
    }

    @Override
    public void customize(ConsulRegistration registration) {
        log.info(">>>>>>>>>> 自定义");
        NewService service = registration.getService();
        List<String> tags = service.getTags();
        if (!tags.isEmpty()) {
            tags.removeIf(tag -> tag.startsWith("secure="));
        }

        String appName = environment.getProperty("spring.application.name", "UNKNOWN");
        String appId = environment.getProperty("cmdb.app.id", appName);

        tags.add("app_id=" + appId);

        service.setTags(tags);
    }
}
